import store from '@/core/stores'
function hasPermission(value, el) {
  if (!Array.isArray(value)) {
    throw new Error(`需要配置权限，例如 v-permission="['getStatistics3,GET']"`)
  }
  const hasAuth =
    value.findIndex((v) => store.state.ruleNames.includes(v)) !== -1
  if (el && !hasAuth) {
    console.log('用户没有权限')
    console.log('用户拥有的权限', store.state.ruleNames.join('--'))
    console.log('当前的节点对外展示的权限', value)
    el?.parentNode && el?.parentNode.removeChild(el)
    console.log('el===>', el)
  }
  return hasAuth
}

export default {
  install(app) {
    console.log('app==>', app)
    app.directive('permission', {
      mounted(el, binding) {
        hasPermission(binding.value, el)
      },
    })
  },
}
